package com.kepler.social.service.manager;

import com.github.pagehelper.PageHelper;
import com.kepler.social.dao.domain.UserBaseInfo;
import com.kepler.social.dao.mapper.UserBaseInfoMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserManager {

    private static final Logger log = LoggerFactory.getLogger(UserManager.class);
    @Resource
    private UserBaseInfoMapper userBaseInfoMapper;

    public UserBaseInfo getById(long id){
        return userBaseInfoMapper.selectByPrimaryKey(id);
    }

    public UserBaseInfo updateAndGet(long id, String name){
        log.info(userBaseInfoMapper.selectByPrimaryKey(id).getNickName());
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setId(id);
        userBaseInfo.setNickName(name);
        userBaseInfoMapper.updateByPrimaryKeySelective(userBaseInfo);
        return userBaseInfoMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public UserBaseInfo updateAndGetTrans(long id, String name){
        log.info(userBaseInfoMapper.selectByPrimaryKey(id).getNickName());
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setId(id);
        userBaseInfo.setNickName(name);
        userBaseInfoMapper.updateByPrimaryKeySelective(userBaseInfo);
        return userBaseInfoMapper.selectByPrimaryKey(id);
    }

    public int update(long id, String name){
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setId(id);
        userBaseInfo.setNickName(name);
        return userBaseInfoMapper.updateByPrimaryKeySelective(userBaseInfo);
    }

    public List<UserBaseInfo> selectAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage( pageNum,  pageSize);
        return userBaseInfoMapper.selectAll();
    }

    public int insert(UserBaseInfo userBaseInfo) {
        return userBaseInfoMapper.insertSelective(userBaseInfo);
    }

    public List<Integer> insertMutile(List<UserBaseInfo> userBaseInfos) {
        return userBaseInfos.stream().map(u -> userBaseInfoMapper.insertSelective(u)).collect(Collectors.toList());
    }
}
